package com.fitness.front.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fitness.front.modal.course.entity.Course;
import com.fitness.front.modal.course.entity.CourseSchedule;
import com.fitness.front.modal.course.params.CourseSchedulePageQueryParams;
import com.fitness.front.modal.course.vo.CourseScheduleVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface CourseScheduleMapper extends BaseMapper<CourseSchedule> {

    @Select("<script>select * from t_course_schedule c "
            + " <where>"
            + "<if test='queryParams.coachName != null and queryParams.coachName.trim() neq \"\"'>"
            + " AND c.coach_name like CONCAT('%',#{queryParams.coachName},'%')"
            + "</if>"
            + "<if test='queryParams.courseName != null and queryParams.courseName.trim() neq \"\"'>"
            + " AND c.course_name like  CONCAT('%',#{queryParams.courseName},'%')"
            + "</if>"
            + "<if test='queryParams.venueName != null and queryParams.venueName.trim() neq \"\"'>"
            + " AND c.venue_name like CONCAT('%',#{queryParams.venueName},'%')"
            + "</if>"
            + "<if test='queryParams.status != null'>"
            + " AND c.status = #{queryParams.status} "
            + "</if>"
            + "<if test='queryParams.startTimeBegin != null and queryParams.startTimeBegin.trim() neq \"\"'>"
            + " AND c.startTime &gt;= #{queryParams.startTimeBegin}  "
            + "</if>"
            + "<if test='queryParams.startTimeEnd != null and queryParams.startTimeEnd.trim() neq \"\"'>"
            + " AND c.startTime &lt;= #{queryParams.startTimeEnd} "
            + "</if>"
            + "<if test='queryParams.endTimeBegin != null and queryParams.endTimeBegin.trim() neq \"\"'>"
            + " AND c.endTime &gt;= #{queryParams.endTimeBegin}  "
            + "</if>"
            + "<if test='queryParams.endTimeEnd != null and queryParams.endTimeEnd.trim() neq \"\"'>"
            + " AND c.endTime &lt;= #{queryParams.endTimeEnd} "
            + "</if>"
            + "</where>"
            +"</script>")
    Page<CourseSchedule> getCourseSchedulePage(Page<CourseSchedule> page, @Param("queryParams") CourseSchedulePageQueryParams queryParams);

    @Select("SELECT tcs.* FROM t_course_schedule tcs, t_member_course tmc " +
            " WHERE tcs.course_id = tmc.course_id AND DATE_FORMAT(tcs.start_time, '%Y-%m') = #{yearMonth} AND tmc.member_id = #{memberId}")
    List<CourseSchedule> getCourseSchedulesByMemberIdAndYearMonth(@Param("memberId") Long memberId, @Param("yearMonth") String yearMonth);

    @Select("SELECT DATE_FORMAT(tcs.start_time,'%Y-%m-%d') FROM t_course_schedule tcs, t_member_course tmc " +
            " WHERE tcs.course_id = tmc.course_id AND DATE_FORMAT(tcs.start_time, '%Y-%m') = #{yearMonth} AND tmc.member_id = #{memberId}")
    List<String> getCourseScheduleStartDate(Long memberId, String yearMonth);

    @Select("select * from t_course_schedule where date_format(start_time,'%Y-%m-%d') = #{queryDate} order by start_time asc")
    List<CourseSchedule> getCourseScheduleDetail(@Param("queryDate") String date);

    @Select("select distinct c.* from t_course c,t_course_schedule cs where cs.coach_id = #{coachId} and c.id = cs.course_id")
    List<Course> getCourseSchedulesByCoachId(@Param("coachId") Long coachId);
}
